package com.industrial.white.handler.excel;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.industrial.system.api.factory.RemoteUserFallbackFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * EasyExcel合并单元格处理类
 * @author sangguangcan
 */

public class ExcelFillCellLineMergeHandler implements RowWriteHandler {
    private static final Logger log = LoggerFactory.getLogger(ExcelFillCellLineMergeHandler.class);

    /**
     * 需要跳过的行索引
     */
    private Integer jumpRowIndex;

    public ExcelFillCellLineMergeHandler(Integer jumpRowIndex) {
        this.jumpRowIndex = jumpRowIndex;
    }

    @Override
    public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                Row row, Integer relativeRowIndex, Boolean isHead) {
        int currentRowIndex = row.getRowNum();
        if(currentRowIndex > jumpRowIndex){
            log.info("插入的行数：" + currentRowIndex);
            mergeCellHandler(writeSheetHolder,row,currentRowIndex);
        }
    }


    /**
     * 合并单元格的操作
     * @param writeSheetHolder
     * @param row
     * @param currentRowIndex
     */
    private void mergeCellHandler(WriteSheetHolder writeSheetHolder, Row row, int currentRowIndex) {
        //获取当前行的 统一社会信用代码
        Cell currentRowUscc = row.getCell(2);
        Object currentUscc = currentRowUscc.getCellType() == CellType.STRING ? currentRowUscc.getStringCellValue() : currentRowUscc.getNumericCellValue();
        //获取上一行的统一社会信用代码
        Cell preRowUscc = row.getSheet().getRow(currentRowIndex - 1).getCell(2);
//        Object preUscc = preRowUscc.getCellType() == CellType.STRING ? preRowUscc.getStringCellValue() : preRowUscc.getNumericCellValue();
        Object preUscc = preRowUscc.getStringCellValue();

        boolean pre = currentUscc.equals(preUscc);
        if(pre) {
            Cell preRowXh = row.getSheet().getRow(currentRowIndex - 1).getCell(0);
            String preXh = preRowXh.getStringCellValue();
            row.getCell(0).setCellValue(preXh);

            //获取到当前的Sheet页
            Sheet sheet = writeSheetHolder.getSheet();
            for (int i = 0; i <= 9; i++) {
                boolean isMerge = false;
                //获取到所有的合并的区域
                List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
                for(int j =0; j < mergedRegions.size() && !isMerge; j++){
                    CellRangeAddress cellAddresses = mergedRegions.get(j);
                    if(cellAddresses.isInRange(currentRowIndex - 1,i)){
                        sheet.removeMergedRegion(j);
                        cellAddresses.setLastRow(currentRowIndex);
                        sheet.addMergedRegion(cellAddresses);
                        isMerge = true;
                    }
                }
                if(!isMerge){
                    CellRangeAddress cellAddresses = new CellRangeAddress(currentRowIndex - 1, currentRowIndex, i, i);
                    sheet.addMergedRegion(cellAddresses);
                }
            }

            for(int i = 18; i <= 24; i++){
                boolean isMerge = false;
                //获取到所有的合并的区域
                List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
                for(int j =0; j < mergedRegions.size() && !isMerge; j++){
                    CellRangeAddress cellAddresses = mergedRegions.get(j);
                    if(cellAddresses.isInRange(currentRowIndex - 1,i)){
                        sheet.removeMergedRegion(j);
                        cellAddresses.setLastRow(currentRowIndex);
                        sheet.addMergedRegion(cellAddresses);
                        isMerge = true;
                    }
                }
                if(!isMerge){
                    CellRangeAddress cellAddresses = new CellRangeAddress(currentRowIndex - 1, currentRowIndex, i, i);
                    sheet.addMergedRegion(cellAddresses);
                }
            }
        } else{
            //获取到上一行的序号
            Cell preRowXh = row.getSheet().getRow(currentRowIndex - 1).getCell(0);
            String preXh = preRowXh.getStringCellValue();
            if(StringUtils.isNotBlank(preXh)){
                Integer pp = Integer.valueOf(preXh) + 1;
                row.getCell(0).setCellValue(String.valueOf(pp));
            }
        }
    }
}
